package ru.amse.tsyganov.jumleditor.view;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;

import ru.amse.tsyganov.jumleditor.model.State;
import ru.amse.tsyganov.jumleditor.model.Transition;

public class TransitionView<T extends Transition> extends ViewElement<Transition> {
	private List<? extends ActivePoint> points;
	
	/*package*/ TransitionView(T model) {
		
		if (model == null) {
			throw new IllegalArgumentException();
		}
		setModel(model);
		points = createActivePoints();
	}
	
	@Override
	public void paint(GC gc) {
		gc.setForeground(Colors.blackColor);
		gc.setBackground(Colors.blackColor);
		
		//if (SelectedItems.items.selectedTransitions.contains(this)) {
		//	gc.setLineWidth(2);
		//}
		TransitionDraughtsman.drawTransition(gc, this);
		//gc.setLineWidth(1);
		
		if (SelectedItems.items.selectedTransitions.contains(this)) {
			for (ActivePoint p : getActivePoints()) {
				p.paintActivePoint(gc);
			}
		}
	}

	@Override
	public void accept(Visitor v) {
		v.visitTransitionView(this);
	}
	
	public Point getStartPoint() {
		return TransitionDraughtsman.getConnectionPoint(
				getTargetCenter(), getSourceRect());
	}
	
	public Point getEndPoint() {
		return TransitionDraughtsman.getConnectionPoint(
				getSourceCenter(), getTargetRect());
	}
	
	public Point getCenterPoint() {
		Point sourceCenter = getSourceCenter();
		Point targetCenter = getTargetCenter();
		return new Point(
				(sourceCenter.x + targetCenter.x) / 2,
				(sourceCenter.y + targetCenter.y) / 2);
	}
	
	private Rectangle getSourceRect() {
		StateView<? extends State> source = ModelViewMap.map.
			getStatesMap().get(getModel().getSource().getContainer());
		return source.getRectangle();
	}
	
	private Rectangle getTargetRect() {
		StateView<? extends State> target = ModelViewMap.map.
			getStatesMap().get(getModel().getTarget().getContainer());
		return target.getRectangle();
	}
	
	private Point getSourceCenter() {
		StateView<? extends State> source = ModelViewMap.map.
			getStatesMap().get(getModel().getSource().getContainer());
		return source.getCenter();
	}
	
	private Point getTargetCenter() {
		StateView<? extends State> target = ModelViewMap.map.
			getStatesMap().get(getModel().getTarget().getContainer());
		return target.getCenter();
	}

	@Override
	public List<? extends ActivePoint> getActivePoints() {
		return points;
	}
	
	private List<? extends ActivePoint> createActivePoints() {
		List<ActivePoint> points = new ArrayList<ActivePoint>(2);
		TransitionActivePoint startPoint = new TransitionActivePoint(true) {
			@Override
			public int getCurrentActivePointX() {
				return getStartPoint().x;
			}
			@Override
			public int getCurrentActivePointY() {
				return getStartPoint().y;
			}
			@Override
			public Transition getModel() {
				return TransitionView.this.getModel();
			}
		};
		
		TransitionActivePoint endPoint = new TransitionActivePoint(false) {
			@Override
			public int getCurrentActivePointX() {
				return getEndPoint().x;
			}
			@Override
			public int getCurrentActivePointY() {
				return getEndPoint().y;
			}
			@Override
			public Transition getModel() {
				return TransitionView.this.getModel();
			}
		};
		points.add(startPoint);
		points.add(endPoint);
		return points;
	}
}
